package com.shayne.demo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Shayne on 2016/7/18.
 */
public class MyAdapter extends ArrayAdapter<User>{

    private int resourceID;
    public MyAdapter(Context context, int resource, List<User> users) {
        super(context, resource, users);
        resourceID = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        User user = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceID, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (ImageView) view.findViewById(R.id.image);
            viewHolder.textView = (TextView) view.findViewById(R.id.text);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.imageView.setImageResource(user.getImageID());
        viewHolder.textView.setText(user.getName());
        return view;
    }
    class ViewHolder{
            ImageView imageView;
            TextView textView;
    }

}
